#include "zh_info_tool.c"

static int _TEST_Split_Str(Sentence_t* pSt, const char* str, size_t size, char (*result)[10], int resultSize)
{
	int rightNum = 0, i = 0;
	memcpy(pSt->pSrcAdr, str, size);
	if(St_SPLIT(pSt))
	{
		while (i < resultSize)
		{
			if(strcmp((const char*)St_GetStr(pSt,i),(const char*)*(result+i)) == 0)
				rightNum++;
			else 
				printf("failed in Line(%d) index(%d),exp:%s,act:%s\n", __LINE__, i, (char*)result[i], (char*)St_GetStr(pSt,i));
			i++;
		}
	}
	if(rightNum == resultSize)
		return 1;
	return 0;
}
#define TEST_Split_Str(pSt, str, result, resultSize) \
        _TEST_Split_Str(pSt, str, sizeof(str), result, resultSize)

static int TEST_Split_Int(Sentence_t* pSt, const char* str, size_t size, long* result, int resultSize)
{
	int rightNum = 0, i = 0;
	memcpy(pSt->pSrcAdr, str, size);
	if(St_SPLIT(pSt))
	{
		while (i < resultSize)
		{
			if(St_GetInt(pSt,i) == result[i])rightNum++;
			else 
				printf("failed in Line(%d) index(%d),exp:%ld,act:%ld\n", __LINE__, i, result[i], St_GetInt(pSt,i));
			i++;
		}
	}
	if(rightNum == resultSize)
		return 1;
	return 0;
}

static int TEST_Split_Double(Sentence_t* pSt, const char* str, size_t size, double* result, int resultSize)
{
	int rightNum = 0, i = 0;
	memcpy(pSt->pSrcAdr, str, size);
	if(St_SPLIT(pSt))
	{
		while (i < resultSize)
		{
			if(fabs(St_GetDob(pSt,i) - result[i]) < 1E-6)rightNum++;
			else 
				printf("failed in Line(%d) index(%d),exp:%lf,act:%lf\n", __LINE__, i, result[i], St_GetDob(pSt,i));
			i++;
		}
	}
	if(rightNum == resultSize)
		return 1;
	return 0;
}

#define TEST_UNIT(x)                                                                 \
	do                                                                               \
	{                                                                                \
		temp = x;                                                                    \
		testSucceeded += temp;                                                       \
		testCount++;                                                                 \
		if (temp == 0)                                                               \
			printf("Test failed in Line(%d) testCount(%d))\n", __LINE__, testCount); \
	} while (0)

Sentence_t pSt1;
static void EQ_TEST(void)
{
	int testSucceeded = 0;
	int testCount = 0;
    char* pstr = NULL;
    int temp = 0;
	char UART1_RBuf[200] = {0};
	char St1_Buf[20];//解析器的缓存
	char result_str[6][10]={{"Hello"}, {"World"}, {"你好"}, {"世界"}, {"Sentence"}, {"nihao"}};
	long result_Int[10] = {123,456,789};
	double result_Double[10] = {123.456,456.789,789.012};
	StInit(&pSt1,"PM1:",',',"\n\r",St1_Buf,UART1_RBuf);//实例化对象
	
	
	/*--------正常数据测试-------*/
	TEST_UNIT(TEST_Split_Str(&pSt1, "PM1:Hello,World,你好,世界,Sentence,nihao\n\r",result_str, 6));
	
	TEST_UNIT(TEST_Split_Int(&pSt1, "PM1:123,456,789\n\r", 18, result_Int, 3));
	
	TEST_UNIT(TEST_Split_Double(&pSt1, "PM1:123.456,456.789,789.012\n\r", 30, result_Double, 3));
	
	/*--------帧尾缺失测试-------*/
	TEST_UNIT(TEST_Split_Str(&pSt1, "PM1:Hello,World,你好,世界,Sentence,nihao\n",result_str, 0));

	TEST_UNIT(TEST_Split_Int(&pSt1, "PM1:123,456,789\r", 17, result_Int, 0));

	TEST_UNIT(TEST_Split_Double(&pSt1, "PM1:123.456,456.789,789.012", 28, result_Double, 0));

	/*--------不完整帧头测试-------*/
	TEST_UNIT(TEST_Split_Str(&pSt1, "PM1M1:P1:PMPPM12:Hello,World,你好,世界,Sentence,nihao\n\r", result_str, 0));

	TEST_UNIT(TEST_Split_Int(&pSt1, "PM1M1:P1:PMPPM12:123,456,789\n\r", 31, result_Int, 0));

	TEST_UNIT(TEST_Split_Double(&pSt1, "PM1M1:P1:PMPPM12:123.456,456.789,789.012\n\r", 43, result_Double, 0));

	/*----------帧头干扰测试---------*/
	TEST_UNIT(TEST_Split_Str(&pSt1, "blabPMlaPMbla PM1:Hello,World,你好,世界,Sentence,nihao\n\r", result_str, 6));

	TEST_UNIT(TEST_Split_Int(&pSt1, "blabPMlaPMbla PM1:123,456,789\n\r", 32, result_Int, 3));

	TEST_UNIT(TEST_Split_Double(&pSt1, "blabPMlaPMbla PM1:123.456,456.789,789.012\n\r", 44, result_Double, 3));

	/*----------空数据测试---------*/
	TEST_UNIT(TEST_Split_Str(&pSt1, "PM1:,,,Hello,World,你好,世界,,,Sentence,nihao\n\r", result_str, 6));

	TEST_UNIT(TEST_Split_Int(&pSt1, "PM1:123,,,456,789,,,\n\r", 23, result_Int, 3));

	TEST_UNIT(TEST_Split_Double(&pSt1, "PM1:123.456,,,456.789,,789.012,\n\r", 33, result_Double, 3));

	/*----------错误数据测试---------*/
	result_Int[0] = 123;result_Int[1] = 45;result_Int[2] = 789;
	TEST_UNIT(TEST_Split_Int(&pSt1, "PM1:123a,45b6,789\n\r", 20, result_Int, 3));
	result_Double[0] = 123.456;result_Double[1] = 456.7;result_Double[2] = 78;
	TEST_UNIT(TEST_Split_Double(&pSt1, "PM1:123.456a,456.7b89,78c9.012\n\r", 33, result_Double, 3));

	printf("test Succeeded: %.1f%%(%d/%d)\n", (float)testSucceeded/testCount*100,testSucceeded,testCount);

}
int main(void)
{
	printf(">> TEST Start\n");
	EQ_TEST();
	printf(">> TEST End\n");
}

